home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / ODF Release 3 / ODFDev / Embed / Sources / Command.cpp < prev    next >
Encoding:
Text File  |  1996-12-16  |  17.4 KB  |  576 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Command.cpp
  4. //    Release Version:    $ ODF 3 $
  5. //
  6. //    Author:                M.Boetcher
  7. //
  8. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "Embed.hpp"
  13.  
  14. #ifndef COMMAND_H
  15. #include "Command.h"
  16. #endif
  17.  
  18. #ifndef SELECT_H
  19. #include "Select.h"
  20. #endif
  21.  
  22. #ifndef PROXY_H
  23. #include "Proxy.h"
  24. #endif
  25.  
  26. #ifndef PART_H
  27. #include "Part.h"
  28. #endif
  29.  
  30. #ifndef FRAME_H
  31. #include "Frame.h"
  32. #endif
  33.  
  34. #ifndef CONTENT_H
  35. #include "Content.h"
  36. #endif
  37.  
  38. // ----- Framework Includes -----
  39.  
  40. #ifndef FWPRESEN_H
  41. #include "FWPresen.h"
  42. #endif
  43.  
  44. // ----- OS Includes -----
  45.  
  46. #ifndef FWORDCOL_H
  47. #include "FWOrdCol.h"
  48. #endif
  49.  
  50. // ----- OpenDoc Includes -----
  51.  
  52. #ifndef SOM_Module_OpenDoc_Commands_defined
  53. #include <CmdDefs.xh>
  54. #endif
  55.  
  56. //========================================================================================
  57. // RunTime Info
  58. //========================================================================================
  59.  
  60. #ifdef FW_BUILD_MAC
  61. #pragma segment opfEmbed
  62. #endif
  63.  
  64. FW_DEFINE_AUTO(CEmbedEditCommand)
  65. FW_DEFINE_AUTO(CEmbedInsertCommand)
  66. FW_DEFINE_AUTO(CEmbedDropCommand)
  67. FW_DEFINE_AUTO(CEmbedDragCommand)
  68.  
  69. //========================================================================================
  70. // CEmbedEditCommand
  71. //========================================================================================
  72.  
  73. //----------------------------------------------------------------------------------------
  74. //    CEmbedEditCommand constructor
  75. //----------------------------------------------------------------------------------------
  76.  
  77. CEmbedEditCommand::CEmbedEditCommand(Environment *ev, 
  78.                                      ODCommandID commandID,
  79.                                      CEmbedContent* content, 
  80.                                      FW_CFrame* frame, 
  81.                                      FW_CSelection* selection,
  82.                                      FW_Boolean canUndo) :
  83.     FW_CClipboardCommand(ev, commandID, frame, canUndo),
  84.         fEmbedContent(content),
  85.         fPastedProxy(NULL),
  86.         fOldProxy(NULL)
  87. {
  88. FW_UNUSED(selection);
  89. }
  90.  
  91. //----------------------------------------------------------------------------------------
  92. //    CEmbedEditCommand destructor
  93. //----------------------------------------------------------------------------------------
  94.  
  95. CEmbedEditCommand::~CEmbedEditCommand()
  96. {
  97. }
  98.  
  99. //----------------------------------------------------------------------------------------
  100. //    CEmbedEditCommand::SaveUndoState
  101. //----------------------------------------------------------------------------------------
  102.  
  103. void CEmbedEditCommand::SaveUndoState(Environment* ev)    // Override
  104. {
  105.     ODCommandID commandID = GetCommandID(ev);
  106.     
  107.     if (commandID == kODCommandCut || commandID == kODCommandClear)
  108.         fPastedProxy = fEmbedContent->GetProxy();
  109.     else if (commandID == kODCommandPaste)
  110.         fOldProxy = fEmbedContent->GetProxy();
  111. }
  112.  
  113. //----------------------------------------------------------------------------------------
  114. //    CEmbedEditCommand::FreeUndoState
  115. //----------------------------------------------------------------------------------------
  116.  
  117. void CEmbedEditCommand::FreeUndoState(Environment* ev)    // Override
  118. {
  119.     ODCommandID commandID = GetCommandID(ev);
  120.     
  121.     if (commandID == kODCommandCut || commandID == kODCommandClear)
  122.     {
  123.         delete fPastedProxy;
  124.     }
  125.     else if (commandID == kODCommandPaste)
  126.     {
  127.         // Delete the original proxy
  128.         delete fOldProxy;
  129.     }
  130. }
  131.  
  132. //----------------------------------------------------------------------------------------
  133. //    CEmbedEditCommand::SaveRedoState
  134. //----------------------------------------------------------------------------------------
  135.  
  136. void CEmbedEditCommand::SaveRedoState(Environment *ev)    // Override
  137. {
  138.     if (GetCommandID(ev) == kODCommandPaste) 
  139.         fPastedProxy = fEmbedContent->GetProxy();
  140. }
  141.  
  142. //----------------------------------------------------------------------------------------
  143. //    CEmbedEditCommand::FreeRedoState
  144. //----------------------------------------------------------------------------------------
  145.  
  146. void CEmbedEditCommand::FreeRedoState(Environment* ev)    // Override
  147. {
  148.     if (GetCommandID(ev) == kODCommandPaste)
  149.     {
  150.         // Delete the pasted proxy
  151.         delete fPastedProxy;
  152.     }
  153. }
  154.  
  155. //----------------------------------------------------------------------------------------
  156. //    CEmbedEditCommand::UndoIt
  157. //----------------------------------------------------------------------------------------
  158.  
  159. void CEmbedEditCommand::UndoIt(Environment *ev)    // Override
  160. {
  161.     FW_CClipboardCommand::UndoIt(ev);
  162.  
  163.     switch (GetCommandID(ev))
  164.     {
  165.         case kODCommandCut:
  166.         case kODCommandClear:
  167.             this->RestorePart(ev);
  168.             break;
  169.  
  170.         case kODCommandPaste:
  171.             this->RemovePart(ev);
  172.             this->RestoreOldPart(ev);
  173.             break;
  174.     }    
  175. }
  176.  
  177. //----------------------------------------------------------------------------------------
  178. //    CEmbedEditCommand::RedoIt
  179. //----------------------------------------------------------------------------------------
  180.  
  181. void CEmbedEditCommand::RedoIt(Environment *ev)    // Override
  182. {
  183.     FW_CClipboardCommand::RedoIt(ev);
  184.  
  185.     switch (GetCommandID(ev))
  186.     {
  187.         case kODCommandCut:
  188.         case kODCommandClear:
  189.             this->RemovePart(ev);
  190.             break;
  191.  
  192.         case kODCommandPaste:
  193.             this->RemovePart(ev);
  194.             this->RestorePart(ev);
  195.             break;
  196.     }    
  197. }
  198.  
  199. //----------------------------------------------------------------------------------------
  200. //    CEmbedEditCommand::RestorePart
  201. //----------------------------------------------------------------------------------------
  202.  
  203. void CEmbedEditCommand::RestorePart(Environment *ev)    // Override
  204. {
  205.     fEmbedContent->SetProxy(fPastedProxy);
  206.     fPastedProxy->AttachEmbeddedFrames(ev);
  207.         
  208.     GetPresentation(ev)->Invalidate(ev);
  209. }
  210.  
  211. //----------------------------------------------------------------------------------------
  212. //    CEmbedEditCommand::RemovePart
  213. //----------------------------------------------------------------------------------------
  214.  
  215. void CEmbedEditCommand::RemovePart(Environment *ev)    // Override
  216. {
  217.     // Clear the selection, which includes detaching the proxy frames.
  218.     fSelection->ClearSelection(ev);
  219. }
  220.  
  221. //----------------------------------------------------------------------------------------
  222. //    CEmbedEditCommand::RestoreOldPart
  223. //----------------------------------------------------------------------------------------
  224.  
  225. void CEmbedEditCommand::RestoreOldPart(Environment* ev)
  226. {
  227.     // restore the previous part, if any
  228.     if (fOldProxy)
  229.     {
  230.         fEmbedContent->SetProxy(fOldProxy);
  231.         fOldProxy->AttachEmbeddedFrames(ev);
  232.  
  233.         // Force a redraw
  234.         GetPresentation(ev)->Invalidate(ev);
  235.     }
  236. }
  237.  
  238. //========================================================================================
  239. //    class CEmbedInsertCommand
  240. //========================================================================================
  241.  
  242. //----------------------------------------------------------------------------------------
  243. //    CEmbedInsertCommand constructor
  244. //----------------------------------------------------------------------------------------
  245.  
  246. CEmbedInsertCommand::CEmbedInsertCommand(Environment* ev,
  247.                                          FW_CEmbeddingFrame* frame,
  248.                                          const FW_PFileSpecification& fileSpec,
  249.                                          CEmbedContent* content)
  250. :    FW_CInsertCommand(ev, frame, fileSpec, FW_kCanUndo),
  251.     fPartContent(content),
  252.     fEmbedSelection(NULL),
  253.     fInsertedProxy(NULL),
  254.     fOldProxy(NULL)
  255. {
  256.     fEmbedSelection = (CEmbedSelection*) frame->GetPresentation(ev)->GetSelection(ev);
  257.     FW_END_CONSTRUCTOR
  258. }
  259.  
  260. //----------------------------------------------------------------------------------------
  261. //    CEmbedInsertCommand destructor
  262. //----------------------------------------------------------------------------------------
  263.  
  264. CEmbedInsertCommand::~CEmbedInsertCommand()
  265. {
  266. }
  267.  
  268. //---------------------------------------------------------------------------------------
  269. //    CEmbedInsertCommand::UndoIt
  270. //---------------------------------------------------------------------------------------
  271.  
  272. void CEmbedInsertCommand::UndoIt(Environment* ev)
  273. {
  274.     // remove the inserted part
  275.     fEmbedSelection->ClearSelection(ev);
  276.  
  277.     // restore the previous part, if any
  278.     fPartContent->SetProxy(fOldProxy);
  279.     if (fOldProxy)
  280.         fOldProxy->AttachEmbeddedFrames(ev);
  281.  
  282.     // Force a redraw
  283.     GetPresentation(ev)->Invalidate(ev);
  284. }
  285.  
  286. //---------------------------------------------------------------------------------------
  287. //    CEmbedInsertCommand::RedoIt
  288. //---------------------------------------------------------------------------------------
  289.  
  290. void CEmbedInsertCommand::RedoIt(Environment* ev)
  291. {
  292.     // remove the original embedded part
  293.     fEmbedSelection->ClearSelection(ev);
  294.  
  295.     // restore the inserted part
  296.     fPartContent->SetProxy(fInsertedProxy);
  297.     fInsertedProxy->AttachEmbeddedFrames(ev);
  298.  
  299.     // Force a redraw
  300.     GetPresentation(ev)->Invalidate(ev);
  301. }
  302.  
  303. //---------------------------------------------------------------------------------------
  304. //    CEmbedInsertCommand::FreeUndoState
  305. //---------------------------------------------------------------------------------------
  306. //    If the fOldProxy is not detached it means that the insert command failed
  307.  
  308. void CEmbedInsertCommand::FreeUndoState(Environment* ev)
  309. {
  310.     if (fOldProxy && fOldProxy->IsDetached(ev))
  311.     {
  312.         // Delete the original proxy
  313.         delete fOldProxy;
  314.         fOldProxy = NULL;
  315.     }
  316. }
  317.  
  318. //---------------------------------------------------------------------------------------
  319. //    CEmbedInsertCommand::FreeRedoState
  320. //---------------------------------------------------------------------------------------
  321.  
  322. void CEmbedInsertCommand::FreeRedoState(Environment* ev)
  323. {
  324. FW_UNUSED(ev);
  325.     // Delete the inserted proxy
  326.     delete fInsertedProxy;
  327. }
  328.  
  329. //---------------------------------------------------------------------------------------
  330. //    CEmbedInsertCommand::SaveUndoState
  331. //---------------------------------------------------------------------------------------
  332.  
  333. void CEmbedInsertCommand::SaveUndoState(Environment* ev)
  334. {
  335. FW_UNUSED(ev);
  336.     // Save the proxy for the part that's currently embedded
  337.     fOldProxy = fPartContent->GetProxy();
  338. }
  339.  
  340. //---------------------------------------------------------------------------------------
  341. //    CEmbedInsertCommand::SaveRedoState
  342. //---------------------------------------------------------------------------------------
  343.  
  344. void CEmbedInsertCommand::SaveRedoState(Environment* ev)
  345. {
  346. FW_UNUSED(ev);
  347.     // Save the proxy for the part that was just inserted
  348.     fInsertedProxy = fPartContent->GetProxy();
  349. }
  350.  
  351. //========================================================================================
  352. //    class CEmbedDropCommand
  353. //========================================================================================
  354.  
  355. //----------------------------------------------------------------------------------------
  356. //    CEmbedDropCommand constructor
  357. //----------------------------------------------------------------------------------------
  358.  
  359. CEmbedDropCommand::CEmbedDropCommand(Environment* ev,
  360.                                      CEmbedContent* content,
  361.                                      FW_CFrame* frame,
  362.                                      ODDragItemIterator* dropInfo, 
  363.                                      ODFacet* odFacet,
  364.                                      const FW_CPoint& dropPoint) : 
  365.     FW_CDropCommand(ev, frame, dropInfo, odFacet, dropPoint, FW_kCanUndo),
  366.     fEmbedContent(content),
  367.     fDroppedProxy(NULL),
  368.     fOldProxy(NULL)
  369. {
  370.     fEmbedSelection = (CEmbedSelection*) frame->GetPresentation(ev)->GetSelection(ev);
  371.     this->SetMenuStrings(ev, "Undo Drop", "Redo Drop");
  372.  
  373.     FW_END_CONSTRUCTOR
  374. }
  375.  
  376. //----------------------------------------------------------------------------------------
  377. //    CEmbedDropCommand destructor
  378. //----------------------------------------------------------------------------------------
  379.  
  380. CEmbedDropCommand::~CEmbedDropCommand()
  381. {
  382. }
  383.  
  384. //---------------------------------------------------------------------------------------
  385. //    CEmbedDropCommand::UndoIt
  386. //---------------------------------------------------------------------------------------
  387.  
  388. void CEmbedDropCommand::UndoIt(Environment* ev)
  389. {
  390.     FW_ASSERT(fDroppedProxy);
  391.  
  392.     // remove the dropped part
  393.     fEmbedSelection->ClearSelection(ev);
  394.  
  395.     // restore the previous part, if any
  396.     fEmbedContent->SetProxy(fOldProxy);
  397.     if (fOldProxy)
  398.         fOldProxy->AttachEmbeddedFrames(ev);
  399.  
  400.     // Force a redraw
  401.     GetPresentation(ev)->Invalidate(ev);
  402. }
  403.  
  404. //---------------------------------------------------------------------------------------
  405. //    CEmbedDropCommand::RedoIt
  406. //---------------------------------------------------------------------------------------
  407.  
  408. void CEmbedDropCommand::RedoIt(Environment* ev)
  409. {
  410.     FW_ASSERT(fDroppedProxy);
  411.  
  412.     // remove the original embedded part
  413.     fEmbedSelection->ClearSelection(ev);
  414.  
  415.     // restore the dropped part
  416.     fEmbedContent->SetProxy(fDroppedProxy);
  417.     fDroppedProxy->AttachEmbeddedFrames(ev);
  418.  
  419.     // Force a redraw
  420.     GetPresentation(ev)->Invalidate(ev);
  421. }
  422.  
  423. //----------------------------------------------------------------------------------------
  424. //    CEmbedDropCommand::SaveUndoState
  425. //----------------------------------------------------------------------------------------
  426.  
  427. void CEmbedDropCommand::SaveUndoState(Environment *ev)
  428. {
  429. FW_UNUSED(ev);
  430.     // Save the proxy for the part that's currently embedded
  431.     fOldProxy = fEmbedContent->GetProxy();
  432. }
  433.  
  434. //---------------------------------------------------------------------------------------
  435. //    CEmbedDropCommand::SaveRedoState
  436. //---------------------------------------------------------------------------------------
  437.  
  438. void CEmbedDropCommand::SaveRedoState(Environment *ev)
  439. {
  440. FW_UNUSED(ev);
  441.     // Save the proxy for the part that was just dropped
  442.     fDroppedProxy = fEmbedContent->GetProxy();
  443. }
  444.  
  445. //---------------------------------------------------------------------------------------
  446. //    CEmbedDropCommand::FreeUndoState
  447. //---------------------------------------------------------------------------------------
  448. //    If the fOldProxy is not detached it means that the drop command failed
  449.  
  450. void CEmbedDropCommand::FreeUndoState(Environment* ev)
  451. {
  452.     if (fOldProxy && fOldProxy->IsDetached(ev))
  453.     {
  454.     
  455.         // Though we're committing a drop in the done state, the dropped proxy may have been removed,
  456.         // and deleted by a subsequent user action.  But if not, we need to access it to call CommitPasteDone
  457.         // for OpenDoc 1.1 in limbo compatility.  More complex parts handle this problem by ref counting.
  458.         // Here, since the part can only ever have one proxy which is 'IN', it's easier just to see if 
  459.         // fProxy is still in the part by checking the part content:
  460.         
  461.         CEmbedContent* content = FW_DYNAMIC_CAST(CEmbedContent, this->GetPart(ev)->GetContent(ev));
  462.         FW_ASSERT(content != NULL);
  463.         if (fDroppedProxy == content->GetProxy())
  464.             fDroppedProxy->CommitPasteDone(ev);
  465.         
  466.         // Delete the original proxy
  467.         delete fOldProxy;
  468.         fOldProxy = NULL;
  469.     }
  470. }
  471.  
  472. //---------------------------------------------------------------------------------------
  473. //    CEmbedDropCommand::FreeRedoState
  474. //---------------------------------------------------------------------------------------
  475.  
  476. void CEmbedDropCommand::FreeRedoState(Environment* ev)
  477. {
  478. FW_UNUSED(ev);
  479.     // Delete the dropped proxy
  480.     delete fDroppedProxy;
  481. }
  482.  
  483. //----------------------------------------------------------------------------------------
  484. //    CEmbedDropCommand::DoDroppedInSameFrame
  485. //----------------------------------------------------------------------------------------
  486.  
  487. FW_Boolean CEmbedDropCommand::DoDroppedInSameFrame(Environment* ev, 
  488.                                               ODStorageUnit* dropSU, 
  489.                                               const FW_CPoint& mouseDownOffset, 
  490.                                               const FW_CPoint& dropPoint)
  491. {
  492.     FW_UNUSED(ev);
  493.     FW_UNUSED(dropSU);
  494.     FW_UNUSED(mouseDownOffset);
  495.     FW_UNUSED(dropPoint);
  496.     return FALSE;
  497. }
  498.  
  499. //========================================================================================
  500. //    class CEmbedDragCommand
  501. //========================================================================================
  502.  
  503. //----------------------------------------------------------------------------------------
  504. //    CEmbedDragCommand constructor
  505. //----------------------------------------------------------------------------------------
  506.  
  507. CEmbedDragCommand::CEmbedDragCommand(Environment* ev,
  508.                                     CEmbedContent* content,
  509.                                     FW_CFrame* frame,
  510.                                     CEmbedSelection* selection) : 
  511.     FW_CDragCommand(ev, frame, FW_kCanUndo),
  512.     fEmbedContent(content),
  513.     fEmbedSelection(selection),
  514.     fSavedProxy(NULL),
  515.     fDraggedProxy(NULL)
  516. {
  517.     fDraggedProxy = content->GetProxy();
  518. }
  519.  
  520. //----------------------------------------------------------------------------------------
  521. //    CEmbedDragCommand destructor
  522. //----------------------------------------------------------------------------------------
  523.  
  524. CEmbedDragCommand::~CEmbedDragCommand()
  525. {
  526. }
  527.  
  528. //---------------------------------------------------------------------------------------
  529. //    CEmbedDragCommand::UndoIt
  530. //---------------------------------------------------------------------------------------
  531.  
  532. void CEmbedDragCommand::UndoIt(Environment* ev)
  533. {
  534.     FW_ASSERT(fSavedProxy);
  535.  
  536.     // Restore saved proxy to the table
  537.     fSavedProxy->AttachEmbeddedFrames(ev);
  538.     fEmbedContent->SetProxy(fSavedProxy);
  539. }
  540.  
  541. //---------------------------------------------------------------------------------------
  542. //    CEmbedDragCommand::RedoIt
  543. //---------------------------------------------------------------------------------------
  544.  
  545. void CEmbedDragCommand::RedoIt(Environment* ev)
  546. {
  547.     fEmbedSelection->ClearSelection(ev);
  548. }
  549.  
  550. //---------------------------------------------------------------------------------------
  551. //    CEmbedDragCommand::SaveUndoState
  552. //---------------------------------------------------------------------------------------
  553.  
  554. void CEmbedDragCommand::SaveUndoState(Environment* ev)
  555. {
  556. FW_UNUSED(ev);
  557.  
  558.     // Save the proxy for the part that was dragged
  559.     fSavedProxy = fDraggedProxy;
  560. }
  561.  
  562. //---------------------------------------------------------------------------------------
  563. //    CEmbedDragCommand::FreeUndoState
  564. //---------------------------------------------------------------------------------------
  565.  
  566. void CEmbedDragCommand::FreeUndoState(Environment* ev)
  567. {
  568. FW_UNUSED(ev);
  569.  
  570. //    fSavedProxy = NULL;
  571.     FW_ASSERT(fSavedProxy);
  572.     delete fSavedProxy;
  573. }
  574.  
  575.  
  576.